<?php
class Node_model_node extends Model {
    protected $table = 'node';
    protected $table_node_type = 'type';


    public function add(
    $user_id = 0, $type = '', $caption = '', 
    $keywords = '' , $description = '' , $url_segment = '', $access_mode = '', $thumbnail = '') {

        $user_id = (int) $user_id;

        $user_id = (int) $user_id;
        if ( ! $user_id) {
            return false;
        }
        $type = (trim($type));
        if (empty($type)){
            return false;
        }

        $caption = trim($caption);
        if (empty($caption)) {
            return false;
        }

        if (empty($url_segment)) {
            $url_segment = tos_clean_url_segment($caption);
        }
        
        
        $has_thumbnail = 0;
        $thumbnail = trim($thumbnail);
        if ( ! empty($thumbnail)) {
            $has_thumbnail = 1;
        }

        $now = time();

        $node_id = $this->db->insert($this->table, array(
            'user_id'       => $user_id,
            'type'          => $type,
            'caption'       => $caption,
            'keywords'      => $keywords,
            'description'   => $description,
            'url_segment'   => $url_segment,
            'access_mode'   => $access_mode,
            'created'       => $now,
            'changed'       => $now,
            'has_thumbnail' => $has_thumbnail
        ));
        
        if ($has_thumbnail === 1) {
            $this->_model('node_image')->add($node_id, 'thumbnail', $thumbnail, $caption);
        }
        
        return $node_id;
    }


    public function update(
    $node_id, $caption = '', $keywords = '', $description = '',
    $url_segment = '', $access_mode = '', $thumbnail = '') {

        $node_id = (int) $node_id;
        if ($node_id <= 0) {
            tos_die('Node update error');
        }

        $data = array();
        $caption = trim($caption);
        $url_segment = trim($url_segment);

        if ( ! empty($caption)) {
            $data['caption'] = $caption;
            if (empty($url_segment)) {
                $url_segment = tos_clean_url_segment($caption);
            }
        }

        if ( ! empty($url_segment)) {
            $data['url_segment'] = $url_segment;
        }

        $keywords = trim($keywords);
        if ( ! empty($keywords)) {
            $data['keywords'] = $keywords;
        }

        $description = trim($description);
        if ( ! empty($description)) {
            $data['description'] = $description;
        }

        $access_mode = trim($access_mode);
        if ( ! empty($access_mode)) {
            $data['access_mode'] = $access_mode;
        }
        
        $thumbnail = trim($thumbnail);
        $has_thumbnail = 0;
        if ( ! empty($thumbnail)) {
            $has_thumbnail = 1;
            $data['has_thumbnail'] = 1;
            $this->_model('node_image')->add($node_id, 'thumbnail', $thumbnail, $caption);
        }

        if ($data) {
            return $this->db->update($this->table, $data, array('id' => $node_id));
        } else {
            return false;
        }


    }


    public function entry($node_id) {
        $node_id = (int) $node_id;
        if ($node_id <= 0) {
            tos_die('node_id error');
        }
        $sql = "SELECT * FROM `{$this->table}` WHERE `id`='{$node_id}' LIMIT 1";
        $row = $this->db->row($sql);
        if ((int) $row['has_thumbnail'] == 1) {
            $row['thumbnail'] = $this->_model('node_image')->entry($node_id, 'thumbnail');
        }
        return $row;
    }



    public function entries($offset = 0, $length = 20, $type = '', $parent_id = -1) {
        $sql = "SELECT * FROM `{$this->table}`";
        $wheres = array();
        $limit_str = '';
        $wheres_str = '';

        $type = trim($type);
        if ( ! empty($type)) {
            $type = $this->db->escape_str($type);
            $wheres[] = "`type`='{$type}'";
        }

        $parent_id = (int) $parent_id;
        if ($parent_id >= 0) {
            $wheres[] = "`parent_id`='{$parent_id}'";
        }
        if ($wheres) {
            $wheres_str = ' WHERE '. implode(' AND ', $wheres);
        }

        $offset = (int) $offset;
        $length = (int) $length;

        $offset = ($offset < 0) ? 0 : $offset;
        $length = ($length <= 0) ? 20 : $length;

        $limit_str = " LIMIT $offset, $length";

        $sql .= $wheres_str . $limit_str;

        $entries = $this->db->rows($sql);

        return $entries;
    }

    /*
     public function insert($node_data, $content_data) {

     print_r($this);

     $this->db->trans_start();
     $node_id = $this->insert_node_data($node_data);
     if (false === $node_id) {
     $this->db->trans_rollback();
     return false;
     }

     $content_data['node_id'] = $node_id;
     $content_id = $this->insert_content_data($content_data);
     if (false === $content_id) {
     $this->db->trans_rollback();
     return false;
     }


     $this->db->trans_commit();
     return $node_id;
     }

     public function insert_node_data($node_data) {
     if ( ! isset($node_data['caption'])) {
     return false;
     }
     return $this->db->insert($this->table_node, $node_data);
     }

     public function insert_content_data($content_data) {
     return false;
     }

     public function get_type_id($typename) {
     $typename = $this->db->escape_str($typename);
     $sql = "SELECT `id` FROM `{$this->table_node_type}` WHERE `typename`='{$typename}' LIMIT 1";
     if ($row = $this->db->row($sql)) {
     return $row['id'];
     }
     return 0;
     }
     */
}
?>